package com.hy.frame.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hy.frame.widget.ui.R;

public class DesignLayoutHelper extends DesignHelper {
    protected boolean designScaleChild;

    public DesignLayoutHelper(@NonNull View target) {
        super(target);
    }

    public void loadAttributes(AttributeSet attrs, int defStyleAttr) {
        if (mTarget == null || mTarget.get() == null) return;
        View v = mTarget.get();
        Context context = v.getContext();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IDesignLayout, defStyleAttr, 0);
        this.designScaleChild = a.getBoolean(R.styleable.IDesignLayout_designScaleChild, false);
        a.recycle();
        super.loadAttributes(attrs, defStyleAttr);
    }

    public void onViewAdded(View child) {
        //是否强制缩放子View
        if (designScaleChild) {
            fitChildView(child);
        }
    }

    public void fitChildView(View child) {
        if (child instanceof IAutoDesign) return;
        //2.1 文本大小
        if (child instanceof TextView) {
            TextView v = (TextView) child;
            v.setTextSize(TypedValue.COMPLEX_UNIT_PX, calDesignSize((int) v.getTextSize()));
        }
        //2.2 padding
        int paddingLeft = child.getPaddingLeft();
        int paddingRight = child.getPaddingRight();
        int paddingTop = calDesignSize(child.getPaddingTop());
        int paddingBottom = calDesignSize(child.getPaddingBottom());
        int paddingStart = child.getPaddingStart();
        int paddingEnd = child.getPaddingEnd();
        if (child.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
            paddingStart = calDesignSize(paddingStart);
            paddingEnd = calDesignSize(paddingEnd);
            child.setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom);
        } else {
            paddingLeft = calDesignSize(paddingLeft);
            paddingRight = calDesignSize(paddingRight);
            child.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        }
        //2.3 如果是ViewGroup继续遍历
        if (child instanceof ViewGroup) {
            fitChildGroup((ViewGroup) child);
        }
    }

    public void fitChildGroup(ViewGroup parent) {
        if (isBreakLayout(parent)) return;
        int size = parent.getChildCount();
        if (size == 0) return;
        for (int i = 0; i < size; i++) {
            View child = parent.getChildAt(i);
            //1. 适配宽高
            fitLayoutParams(child.getLayoutParams());
            //2. 子控件适配
            fitChildView(child);
        }
    }

    public void fitLayoutParams(ViewGroup.LayoutParams params) {
        if (params == null) return;
        if (params.width > 0) {
            params.width = calDesignSize(params.width);
        }
        if (params.height > 0) {
            params.height = calDesignSize(params.height);
        }
        if (params instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) params;
            if (marginParams.topMargin != 0) {
                marginParams.topMargin = calDesignSize(marginParams.topMargin);
            }
            if (marginParams.bottomMargin != 0) {
                marginParams.bottomMargin = calDesignSize(marginParams.bottomMargin);
            }
            //优先 以left和right 为主
            if (marginParams.leftMargin != 0 || marginParams.rightMargin != 0) {
                marginParams.leftMargin = calDesignSize(marginParams.leftMargin);
                marginParams.rightMargin = calDesignSize(marginParams.rightMargin);
            } else {
                if (marginParams.getMarginStart() != 0) {
                    marginParams.setMarginStart(calDesignSize(marginParams.getMarginStart()));
                }
                if (marginParams.getMarginEnd() != 0) {
                    marginParams.setMarginEnd(calDesignSize(marginParams.getMarginEnd()));
                }
            }
        }
    }

}
